package org.robert.export_client.excel.impl;

import com.alibaba.fastjson.JSON;
import org.ehcache.impl.internal.concurrent.ConcurrentHashMap;
import org.robert.export_client.excel.entity.ExcelConstant;
import org.robert.export_client.excel.metadata.ExcelHeadEntity;
import org.robert.export_client.excel.metadata.IBaseCsvWrite;
import org.robert.export_client.excel.util.ExcelUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * CsvWriteByDataService:
 *
 * @author zhaoshengtao
 * @date 2024/02/28
 */
public class CsvWriteByDataService implements IBaseCsvWrite {

    private final Map<Object, Object> jsonCacheMap = new ConcurrentHashMap<>();

    private int idSeq = 1;
    /**
     * 数据格式
     * @param data
     * @param clazz
     * @param propertyNames
     * @throws Exception
     */
    @Override
    public List<String[]> doWrite(List<?> data, Class<?> clazz, List<ExcelHeadEntity> propertyNames) throws Exception {
        List<String[]> list = new ArrayList<>();
        int i = this.idSeq;
        // 先设置头
        list.add(propertyNames.stream().map(ExcelHeadEntity::getColumnName).collect(Collectors.toList()).toArray(new String[propertyNames.size()]));

        String[] array = new String[propertyNames.size()];
        // 遍历实体对象集合
        for (Object t : data) {
            int j = 0;
            for (ExcelHeadEntity headEntity : propertyNames) {
                String fieldName = headEntity.getFieldName();
                boolean isJson = headEntity.isJson();
                if(isJson){
                    Optional<Field> field = Optional.ofNullable(FieldUtil.getFieldByName(clazz, fieldName));
                    if (field.isPresent()) {
                        field.get().setAccessible(true);
                        Object value = field.get().get(t);
                        if (value instanceof String) {
                            // 解析json
                            Class<?> jsonClazz = headEntity.getJsonClazz();
                            // 构建一个缓存池
                            Object jsonValue = this.jsonCacheMap.computeIfAbsent(value, k -> JSON.parseObject((String) k, jsonClazz));
                            String subFieldName = headEntity.getSubFieldName();
                            String fieldValue = FieldUtil.setFiledByName(subFieldName, headEntity.getJsonClazz(), jsonValue, headEntity);
                            array[j] = fieldValue;
                            j = j + 1;
                            continue;
                        }
                    }
                }else{
                    // 判断是否需要多字段拼接
                    String fieldValue = "";
                    if(headEntity.isConnect()){
                        fieldValue = FieldUtil.setFiledByNames(clazz, t, headEntity);
                    }else{
                        fieldValue = FieldUtil.setFiledByName(fieldName, clazz, t, headEntity);
                    }
                    array[j] = fieldValue;
                }
                j ++;
            }
        }
        list.add(array);
        this.idSeq += data.size();
        return list;
    }
}
